package syf.stack;

import java.util.Scanner;

public class Stack {
    public static void main(String[] args) {
        ArrayStack arrayStack=new ArrayStack();
        arrayStack.CreatStack(4);
        boolean loop=true;
        String input;
        Scanner scanner=new Scanner(System.in);
        int value;
        while (loop){
            System.out.println("show:   显示队列信息");
            System.out.println("pop:    出栈");
            System.out.println("push:   入栈");
            System.out.println("exit:   退出");
            System.out.println("请输入");
            input=scanner.next();
            switch(input){
                case "show":arrayStack.show();break;
                case "pop": arrayStack.pop();break;
                case "push":{
                    System.out.println("请输入要入栈的数");
                    value=scanner.nextInt();
                    arrayStack.push(value);
                    break;
                }
                case "exit":loop=false;break;
                default:
                    System.out.println("请输入正确的值");;
            }
        }
    }
}
class ArrayStack{
    private int top=-1;//初始为-1
    private int MaxSize;//栈的大小
    private int[] stack;//数组模拟栈
    //创建栈
    public void CreatStack(int MaxSize){
        this.MaxSize=MaxSize;
        stack=new int[MaxSize];
    }
    //判断是否为空
    public boolean isEmpty(){
        return top==-1;
    }
    //判断是否为满
    public boolean isFull(){
        return top==MaxSize-1;
    }
    //入栈
    public void push(int value){
        if (isFull()){
            System.out.println("栈满了");
            return;}
        stack[++top]=value;
    }
    //出栈
    public int pop(){
        if (isEmpty())
            throw new RuntimeException("栈已经空了");
        return stack[top--];
    }
    //遍历栈
    public void show(){
        if (isEmpty()){

            return;}
        for (int i=top;i>=0;i--){
            System.out.println("栈元素为"+stack[i]);
        }
    }
}
